package cn.com.pc.content.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

    public static final int YEAR = Calendar.YEAR;
    public static final int MONTH = Calendar.MONTH;
    public static final int DATE = Calendar.DATE;
    public static final int HOUR = Calendar.HOUR;
    public static final int MINUTE = Calendar.MINUTE;
    public static final int SECOND = Calendar.SECOND;

    //线程安全
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");


    public static String toDateString(Date date) {
        LocalDateTime dateTime = LocalDateTime.ofEpochSecond(
                date.getTime()/1000L, 0, ZoneOffset.ofHours(8));
        return dateTime.format(FORMATTER);
    }

    //getTimeBeforeDay
    public static String getTimestampString(int days) {// Timestamp
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, days);
        return DateTimeFormat.format(cal.getTime());
    }

    //getDayString
    public static String getDateString(int days) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, days);
        return DateFormat.format(cal.getTime());
    }

    //getDay
    public static Date getTimestamp(int days) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, days);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
        return cal.getTime();
    }

    /**
     * yyyy-mm-dd 00:00:00
     *
     * @param days
     * @return yyyy-mm-dd 00:00:00
     */
    public static Date getDate(int days) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, days);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0);

        String dateStr = DateTimeFormat.format(cal.getTime());
        Date date = null;
        try {
            date = DateTimeFormat.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String toDate(Date date) {
        if (date == null) {
            return "";
        }
        return DateTimeFormat.format(date);
    }

    public static String toDate0(Date date) {
        String s = toDate(date);
        s += (s.trim().length() > 0 ? " 00:00:00" : "");
        return s;
    }

    public static String toTime(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        return df.format(date);
    }

    public static String toDataStr(Date date) {
        if (date == null) {
            return "";
        }
        return DateFormat.format(date);
    }

    public static Timestamp toTimestamp(Date date) {
        if (date == null) {
            return null;
        }
        return Timestamp.valueOf(DateTimeFormat.format(date));
    }

    public static Date getFirstdayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);
        return cal.getTime();
    }

    public static Date getLastdayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, 1, 23, 59, 59);
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }

    //得到下个月的开始
    public static Date getFirstdayOfNextMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
        return cal.getTime();
    }

    //得到上一个星期的开始(星期日为一周的开始)
    public static Date getLastWeekStart() {
        Calendar cld = Calendar.getInstance();
        cld.add(Calendar.DATE, -(cld.get(Calendar.DAY_OF_WEEK) - 1));
        cld.add(Calendar.DATE, -7);
        cld.set(cld.get(Calendar.YEAR), cld.get(Calendar.MONTH), cld.get(Calendar.DATE), 0, 0, 0);
        return cld.getTime();
    }

    //得到上一个星期的开始(星期一为一周的开始)
    public static Date getLastWeekStart2() {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);

        Calendar cld = Calendar.getInstance();
        cld.add(Calendar.DATE, -7);
        c.setTime(cld.getTime());

        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    //得到上一个星期的结束(星期日为一周的开始)
    public static Date getLastWeekEnd() {
        Calendar cld = Calendar.getInstance();
        cld.add(Calendar.DATE, -(cld.get(Calendar.DAY_OF_WEEK) - 1));
        cld.add(Calendar.DATE, -1);
        cld.set(cld.get(Calendar.YEAR), cld.get(Calendar.MONTH), cld.get(Calendar.DATE), 23, 59, 59);
        return cld.getTime();
    }

    //取得当前日期是多少周(201035)
    public static int getWeekOfYear2(Date date) {
        Calendar c = Calendar.getInstance();
        //设置星期天为一周的开始
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        //设置只有一天，也当成是一周(如：2011年1月1日是2011年的第一周)
        c.setMinimalDaysInFirstWeek(1);
        c.setTime(date);
        SimpleDateFormat tsFormat = new SimpleDateFormat("MM");
        //特殊处理最后一周(不在同一年)的情况
        if ("12".equals(tsFormat.format(date)) && c.get(Calendar.WEEK_OF_YEAR) == 1) {
            if (c.get(Calendar.WEEK_OF_YEAR) < 10) {
                return Integer.parseInt((c.get(Calendar.YEAR) + 1) + "0" + c.get(Calendar.WEEK_OF_YEAR));
            } else {
                return Integer.parseInt((c.get(Calendar.YEAR) + 1) + "" + c.get(Calendar.WEEK_OF_YEAR));
            }
        } else {
            if (c.get(Calendar.WEEK_OF_YEAR) < 10) {
                return Integer.parseInt(c.get(Calendar.YEAR) + "0" + c.get(Calendar.WEEK_OF_YEAR));
            } else {
                return Integer.parseInt(c.get(Calendar.YEAR) + "" + c.get(Calendar.WEEK_OF_YEAR));
            }
        }
    }

    /**
     * 获取指定星期的开始时刻00:00:00.000
     *
     * @param interval 年份拼周数如201008
     * @return Date java.util.Date
     */
    public static Date getForstdayOfWeek(int interval) {
        Calendar cld = Calendar.getInstance();
        cld.set(Calendar.YEAR, interval / 100);
        cld.set(Calendar.WEEK_OF_YEAR, interval % 100);
        cld.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cld.set(cld.get(Calendar.YEAR), cld.get(Calendar.MONTH), cld.get(Calendar.DATE), 0, 0, 0);
        return cld.getTime();
    }

    /**
     * 获取指定星期的结束时刻00:00:00.000
     *
     * @param interval 年份拼周数如201008
     * @return Date java.util.Date
     */
    public static Date getLastdayOfWeek(int interval) {
        Calendar cld = Calendar.getInstance();
        cld.set(Calendar.YEAR, interval / 100);
        cld.set(Calendar.WEEK_OF_YEAR, interval % 100 + 1);
        cld.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cld.set(cld.get(Calendar.YEAR), cld.get(Calendar.MONTH), cld.get(Calendar.DATE) - 1, 23, 59, 59);
        return cld.getTime();
    }

    /**
     * 获取指定月的开始时刻00:00:00.000
     *
     * @param interval 年份拼月份如201008
     * @return Date java.util.Date
     */
    public static Date getForstdayOfMonth(int interval) {
        Calendar cld = Calendar.getInstance();
        cld.set(interval / 100, interval % 100 - 1, 1, 0, 0, 0);
        return cld.getTime();
    }

    /**
     * 获取指定月的结束时刻00:00:00.000
     *
     * @param interval 年份拼月份如201008
     * @return Date java.util.Date
     */
    public static Date getLastdayOfMonth(int interval) {
        Calendar cld = Calendar.getInstance();
        cld.set(Calendar.YEAR, interval / 100);
        cld.set(Calendar.MONTH, interval % 100);
        cld.set(Calendar.DAY_OF_MONTH, 1);
        cld.set(cld.get(Calendar.YEAR), cld.get(Calendar.MONTH), cld.get(Calendar.DATE) - 1, 23, 59, 59);
        return cld.getTime();
    }

    /**
     * 获取指定时间的年份周数201035
     *
     * @return Date java.util.Date
     */
    public static int getWeekOfYear(Date date) {
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        return cld.get(Calendar.YEAR) * 100 + cld.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取指定时间的年份月份201005
     *
     * @return Date java.util.Date
     */
    public static int getMonthOfYear(Date date) {
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        return cld.get(Calendar.YEAR) * 100 + cld.get(Calendar.MONTH) + 1;
    }

    public static Date getBeginLastDay(int days) {
        Calendar cld = Calendar.getInstance();
        cld.set(Calendar.DATE, cld.get(Calendar.DATE) - days);
        cld.set(cld.get(Calendar.YEAR), cld.get(Calendar.MONTH), cld.get(Calendar.DATE), 0, 0, 0);
        return cld.getTime();
    }

    public static Date getEndLastDay(int days) {
        Calendar cld = Calendar.getInstance();
        cld.set(Calendar.DATE, cld.get(Calendar.DATE) - days);
        cld.set(cld.get(Calendar.YEAR), cld.get(Calendar.MONTH), cld.get(Calendar.DATE), 23, 59, 59);
        return cld.getTime();
    }

    /**
     * 获取某天的开始时刻00:00:00.000
     *
     * @param date 需要获取天内的时间
     * @return Date java.util.Date
     */
    public static Date getStartOfDay(Date date) {
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        cld.set(Calendar.HOUR_OF_DAY, 0);
        cld.set(Calendar.MINUTE, 0);
        cld.set(Calendar.SECOND, 0);
        cld.set(Calendar.MILLISECOND, 0);
        return cld.getTime();
    }

    /**
     * 获取某天的结束时刻23:59:59.000
     *
     * @param date 需要获取天内的时间
     * @return Date java.util.Date
     */
    public static Date getEndOfDay(Date date) {
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        cld.set(Calendar.HOUR_OF_DAY, 23);
        cld.set(Calendar.MINUTE, 59);
        cld.set(Calendar.SECOND, 59);
        cld.set(Calendar.MILLISECOND, 0);
        return cld.getTime();
    }

    /**
     * 获取某小时的开始时刻00:00.000
     *
     * @param date 需要获取天内的时间
     * @return Date java.util.Date
     */
    public static Date getStartOfHours(Date date) {
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        cld.set(Calendar.MINUTE, 0);
        cld.set(Calendar.SECOND, 0);
        cld.set(Calendar.MILLISECOND, 0);
        return cld.getTime();
    }

    public static Date getStartMidOfHours(Date date) {
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        cld.set(Calendar.MINUTE, 30);
        cld.set(Calendar.SECOND, 0);
        cld.set(Calendar.MILLISECOND, 0);
        return cld.getTime();
    }

    /**
     * 获取某小时的结束时刻59:59.000
     *
     * @param date 需要获取天内的时间
     * @return Date java.util.Date
     */
    public static Date getEndOfHours(Date date) {
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        cld.set(Calendar.MINUTE, 59);
        cld.set(Calendar.SECOND, 59);
        cld.set(Calendar.MILLISECOND, 0);
        return cld.getTime();
    }

    public static Date getEndMidOfHours(Date date) {
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        cld.set(Calendar.MINUTE, 29);
        cld.set(Calendar.SECOND, 59);
        cld.set(Calendar.MILLISECOND, 0);
        return cld.getTime();
    }

    //获得当前系统时间
    public static String getNow() {
        Date date = new Date();
        String dates = DateTimeFormat.format(date);
        return dates;
    }

    //根据格式化标准获取时间
    public static String getTime(String format) {
        Date date = new Date();
        java.text.DateFormat sdf = new SimpleDateFormat(format);
        String dates = sdf.format(date);
        return dates;
    }

    //比较两个时间相差的天数，正数为strDate2大于strDate1
    public static int diffDay(Date d1, Date d2) {
        int day = 0;
        if (d1 != null && d2 != null) {
            day = (int) ((d2.getTime() - d1.getTime()) / 1000 / 3600 / 24);
        }
        return day;
    }

    public static boolean judgeDiffDay(Date time, int day) {
        int diff = diffDay(time, new Date());
        if (diff <= day) {
            return true;
        } else {
            return false;
        }
    }

    //比较两个时间相差的小时数，正数为strDate2大于strDate1
    public static Long diffHour(Date strDate1, Date strDate2) {
        Long hours = 0L;
        if (strDate1 != null && strDate2 != null) {
            hours = (Long) ((strDate2.getTime() - strDate1.getTime()) / 1000 / 3600);
        }
        return hours;
    }

    //比较两个时间相差的分钟数，正数为strDate2大于strDate1
    public static Long diffMinutes(Date strDate1, Date strDate2) {
        Long minutes = 0L;
        if (strDate1 != null && strDate2 != null) {
            minutes = (Long) ((strDate2.getTime() - strDate1.getTime()) / 1000 / 60);
        }
        return minutes;
    }

    //得到指定时间,n是数字,正数是将来的，负数是以前的，type是定义对那部分的:年月日，format格式化样式
    public static String getTargetTime(int n, int type, String format) {
        Calendar c = Calendar.getInstance();
        java.text.DateFormat df = new SimpleDateFormat(format);
        c.setTime(new Date());
        c.add(type, n);
        Date d2 = c.getTime();
        String s = df.format(d2);
        return s;
    }

    public static Date getToday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    //毫秒转换成分或者小时
    public static String MillisecondToDate(long time) {
        StringBuilder buf = new StringBuilder();
        long h = time / (60 * 60 * 1000);
        long mm = time % (60 * 60 * 1000);
        long m = mm / (60 * 1000);
        long ss = mm % (60 * 1000);
        long s = ss / 1000;
        if (h > 0) {
            if (h < 10) {
                buf.append("0" + h).append(":");
            } else {
                buf.append(h).append(":");
            }

        }
        if (m > 0) {
            if (m < 10) {
                buf.append("0" + m).append(":");
            } else {
                buf.append(m).append(":");
            }
            if (s <= 0) {
                buf.append("00");
            }

        }
        if (s > 0) {
            if (s < 10) {
                if (m <= 0) {
                    buf.append("00:0" + s);
                } else {
                    buf.append("0" + s);
                }

            } else {
                if (m <= 0) {
                    buf.append("00:" + s);
                } else {
                    buf.append(s);
                }

            }

        }
        return buf.toString();
    }

    /**
     * @param date1 <String>
     * @param date2 <String>
     * @return int
     * @throws ParseException
     */
    public static int getMonthInterval(String date1, String date2) {
        int result = 0;
        try {
            SimpleDateFormat sdf = DateTimeFormat;
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));
            result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
            return result == 0 ? 1 : Math.abs(result);
        } catch (Exception e) {
        }
        return result;

    }

    /**
     * 增加不同年份的月份相差，必须date1<date2
     *
     * @param date2 <Date>
     * @return int
     * @throws ParseException
     * @ param date1 <Date>
     */
    public static int getMonthInterval(Date date1, Date date2) {
        if (date1.after(date2)) {
            throw new RuntimeException("date1 must be before date2 ");
        }
        int result = 0;
        try {
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(date1);
            c2.setTime(date2);
            result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
            int years = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
            if (years > 0) {
                result = result + 12 * years;
            }
            return result;
        } catch (Exception e) {
        }
        return result;

    }

    /**
     * 取得当前日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 取得当前日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    //获取当前月的第一天
    public static Date getFirstDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    //获取当前月的最后一天

    public static Date getLastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 取得当前日期所在自然周的第一天->星期天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfNormalWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); //Sunday
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 取得当前日期所在自然周的最后一天->星期六
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfNormalWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Saturday
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 日期类型转换成字符类型
     *
     * @param date 要转换的日期
     * @return
     */
    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    /**
     * 日期类型转换成字符类型
     *
     * @param date 要转换的日期
     * @return
     */
    public static String formatDate(Date date, String patten) {
        if ("".equals(patten)) {
            patten = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(patten);
        return sdf.format(date);
    }

    /**
     * 是否周日
     *
     * @param date
     * @return
     */
    public static boolean isSunday(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        c.setTime(date);
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {//星期日
            return true;
        }
        return false;
    }

    /**
     * 是否周一
     *
     * @param date
     * @return
     */
    public static boolean isMonday(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        c.setTime(date);
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {//星期一
            return true;
        }
        return false;
    }

    /**
     * 取某一天的昨天
     *
     * @param today 某一天
     * @return
     */
    public static Date getYesterday(Date today) {
        long t = today.getTime();
        Date date = new Date(t - 24 * 60 * 60 * 1000L);
        try {
            date = DateTimeFormat.parse(DateTimeFormat.format(date));
        } catch (ParseException e) {

        }
        return date;
    }

    /**
     * 判断当前日期是本周的星期几,星期一返回1,星期二返回2,...星期日返回7
     *
     * @param date 传进来的当前日期
     * @return
     */
    public static int dayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        c.setTime(date);
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            return 7;
        } else {
            return c.get(Calendar.DAY_OF_WEEK) - 1;
        }
    }

    //n天前或后 + -
    public static Date addDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, day);

        return new Date(cal.getTime().getTime());
    }

    //n月前或后 + -
    public static Date addMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);

        return new Date(cal.getTime().getTime());
    }

    //n小时前或后 + -
    public static Date addHour(Date date, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);
        return new Date(cal.getTime().getTime());
    }

    public static Date parse(String text) {
        return parse(text, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date parse(String string, String fromat) {
        SimpleDateFormat sdf = new SimpleDateFormat(fromat);
        try {
            return sdf.parse(string);
        } catch (ParseException e) {
            return null;
        }
    }

}
